import axios from '@/config/lib/axios'
import Default from '@/config/default'
import Utils from '@/utils'
/*
 * 系统附件api
 */
export default class file {
  // 上传文件地址
  static UPLOAD_URL = '/api/system/file/upload'
  // 上传文件夹地址前缀
  static UPLOAD_FOLDER_URL = '/api/system/file/{s}/upload'

  /**
   * 查询文件列表
   * @param filter 过滤器
   */
  static list (filter) {
    return axios({
      url: '/system/file/list',
      methods: Default.HTTP_METHOD.GET,
      params: filter
    })
  }

  /**
   * 上传文件
   * @param file 待上传文件
   */
  static upload (file) {
    const data = new FormData()
    data.append('file', file)
    return axios({
      url: '/system/file/upload',
      methods: Default.HTTP_METHOD.POST,
      data,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }

  /**
   * 删除文件
   * @param fileKey 文件key值
   */
  static delete (fileKey) {
    return axios({
      url: Utils.formatStr('/system/file/delete/{s}', fileKey),
      method: Default.HTTP_METHOD.DELETE
    })
  }

  /**
   * 下载文件
   * @param fileKey 文件key值
   */
  static download (fileKey) {
    return axios({
      url: Utils.formatStr('/system/file/download/{s}', fileKey),
      method: Default.HTTP_METHOD.GET,
      responseType: 'blob'
    })
  }

  /**
   * 查询文件夹目录
   */
  static folderTree () {
    return axios({
      url: '/system/file/folder/tree',
      method: Default.HTTP_METHOD.GET
    })
  }

  /**
   * 新增文件夹
   */
  static addFolder (sysFile) {
    return axios({
      url: '/system/file/add/folder',
      method: Default.HTTP_METHOD.PUT,
      data: sysFile
    })
  }

  /**
   * 单一文件操作，删除|delete，移动|move，复制|copy，重命名|rename
   */
  static operate (operate, sysFile) {
    return axios({
      url: Utils.formatStr('/system/file/operate/{s}', operate),
      method: Default.HTTP_METHOD.POST,
      data: sysFile
    })
  }

  /**
   * 批量文件操作，删除|delete，移动|move，复制|copy，重命名|rename
   */
  static batchOperate (operate, sysFileList) {
    return axios({
      url: Utils.formatStr('/system/file/batch/operate/{s}', operate),
      method: Default.HTTP_METHOD.POST,
      data: sysFileList
    })
  }
}
